package com.techstar.ats.netty.handler;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import com.techstar.ats.MessageType;
import com.techstar.ats.dxp.AtsMessage;
import com.techstar.ats.dxp.live.rep.F09TrainIndicationInit;
import com.techstar.ats.dxp.live.rep.F10TrainIndicationUpdate;
import com.techstar.ats.dxp.live.rep.F11TrainIndicationRemove;
import com.techstar.ats.service.LiveService;

@Slf4j
@Sharable
@RequiredArgsConstructor
public class LiveHandler extends ChannelInboundHandlerAdapter {
	private final LiveService service;

	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		if(msg instanceof AtsMessage) {
			AtsMessage am = (AtsMessage) msg;
			switch(MessageType.valueOf(am.id)) {
				case TRAIN_INDICATION_INIT:
					service.procInit((F09TrainIndicationInit) msg);
					break;
				case TRAIN_INDICATION_UPDATE:
					service.procUpdate((F10TrainIndicationUpdate) msg);
					break;
				case TRAIN_INDICATION_REMOVE:
					service.procRemove((F11TrainIndicationRemove) msg);
			}
		}

		super.channelRead(ctx, msg);
	}
}
